package com.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ophiux.wechat.db.MchAppModel;
import com.ophiux.wechat.db.Mch_App_Map;
import com.ophiux.wechat.model.CM_UnifiedOrderRequest;
import com.ophiux.wechat.model.PayJsModel;
import com.ophiux.wechat.model.SP_UnifiedOrderRequest;
import com.ophiux.wechat.model.UnifiedOrderResponse;
import com.ophiux.wechat.utils.CommonUtil;
import com.ophiux.wechat.utils.StringUtil;
import com.ophiux.wechat.utils.WeChatPayUtil;
import com.utils.PayUtils;

/**
 * 微信服务商模式-扫码支付生成入口
 * @author chenh
 *
 */
public class NativeUnifiedOrderServlet extends HttpServlet{

	private static Logger logger = LoggerFactory.getLogger(NativeUnifiedOrderServlet.class);
	
	/**
	 * 服务商基本配置信息
	 * 暂时配置为重庆奥菲科服务商信息
	 */
	private static String appid = "wx7ffdd8b451235141";
	private static String appsecret = "dc3f53eda7e0015643e1280572ac1464";
	private static String api_key = "1ids5eohkq5wcb8sq7qievtp9fs8juyz";
	private static String mch_id = "1367185002";
	
	/**
	 * 服务器外网IP
	 */
	private static String Spbill_create_ip = "192.168.1.10";
	
	/**
	 * 支付类型
	 */
	private static String Trade_type = "NATIVE";
	
	/**
	 * 测试地址
	 * localhost:8080/OphiuxWeChatPayPlatformbuild/NativeUnifiedOrderServlet?orderNo=test32435454w4323&money=100&subMchid=1394076602&notifyUrl=http://wechatpay.ophiux.com/OphiuxWeChatPayPlatformbuild/weChatPay
	 * 样例：
	 * <xml>
	 * <return_code><![CDATA[SUCCESS]]>
	 * </return_code><return_msg><![CDATA[OK]]>
	 * </return_msg><appid><![CDATA[wx7ffdd8b451235141]]>
	 * </appid><mch_id><![CDATA[1367185002]]></mch_id>
	 * <sub_mch_id><![CDATA[1394076602]]></sub_mch_id>
	 * <nonce_str><![CDATA[2HS22NNSYsRASU7O]]></nonce_str>
	 * <sign><![CDATA[11CD2B79F2EA156D46EEFEB51E714E6E]]></sign>
	 * <result_code><![CDATA[SUCCESS]]></result_code>
	 * <prepay_id><![CDATA[wx20161219181055dc6524da2a0000174084]]></prepay_id>
	 * <trade_type><![CDATA[NATIVE]]></trade_type>
	 * <code_url><![CDATA[weixin://wxpay/bizpayurl?pr=RiEPdDd]]></code_url></xml>
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		/**
		 * 授权发起页面传来的固定参数
		 */
		logger.info("-----------------进入扫码支付下单，获取前端参数-----------------------");
		String orderNo = request.getParameter("orderNo"); 	
		String money = request.getParameter("money");
		 
		String subMchid = request.getParameter("subMchid");
		String notifyUrl = request.getParameter("notifyUrl");
		 
		 /**
		  * 商户信息,根据MchId获取
		  */
//		 MchAppModel mchModel = Mch_App_Map.getMchAppModelByMchId(subMchid);
//		 
//		 String appid = null;
//	     String appsecret = null;
//	     String api_key = null;
//	     /**
//	      * 商户信息存在
//	      */
//	     if(mchModel != null){
//			 appid = mchModel.getAppid();
//			 appsecret = mchModel.getAppsecret();
//			 api_key = mchModel.getApi_key();
//		 }
		
		/**
		 * 生成预支付下单请求实体
		 */
		 SP_UnifiedOrderRequest sp_unifiedOrderRequest = new SP_UnifiedOrderRequest();
		 sp_unifiedOrderRequest.setAppid(appid);
		 sp_unifiedOrderRequest.setMch_id(mch_id);
		 sp_unifiedOrderRequest.setSub_mch_id(subMchid);
		 sp_unifiedOrderRequest.setNonce_str(CommonUtil.getNonceStr());
		 //预约挂号/门诊支付
		 sp_unifiedOrderRequest.setBody("预约挂号/门诊缴费");
		 sp_unifiedOrderRequest.setAttach("");
		 sp_unifiedOrderRequest.setOut_trade_no(orderNo);
		 sp_unifiedOrderRequest.setProduct_id(orderNo);
		 sp_unifiedOrderRequest.setTotal_fee(PayUtils.getMoney(money));
		 sp_unifiedOrderRequest.setSpbill_create_ip(Spbill_create_ip);
		 sp_unifiedOrderRequest.setNotify_url(notifyUrl);
		 sp_unifiedOrderRequest.setTrade_type(Trade_type);
		 
		/**
		 * 调用微信普通商户统一下单
		 */
		 UnifiedOrderResponse unifiedOrder = WeChatPayUtil.Native_UnifiedOrder(sp_unifiedOrderRequest, api_key) ;
		 
		 if(unifiedOrder == null){
			 response.getWriter().write("");
		 }
		 ObjectMapper obj=new ObjectMapper();
		 String res  = obj.writeValueAsString(unifiedOrder);
		 System.out.println(res);
		 response.getWriter().write(res);
		 
	}

	 
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
	
}
